<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/27
 * Time: 15:17
 */

namespace app\api\controller;


use app\model\LeftMenuModel;
use app\model\StaffModel;
use think\facade\Request;
use think\facade\Validate;

class LeftMenu
{
    //获取后台左侧菜单列表
    public function list()
    {
        $leftMenuList=LeftMenuModel::select();
        foreach ($leftMenuList as &$value){
            //操作员
            $staffOne = StaffModel::find($value['staff_id']);
            $value['username'] = $staffOne['username'] ?? 'null';
        }
        unset($value);
        $data = [
            'status' => 0,
            'message' => '',
            'data' => [
                'leftMenuList' => $leftMenuList
            ],
        ];
        return json($data);
    }
    
    //后台左侧菜单增加
    public function add()
    {
        $params = Request::param();
        
        $validate = Validate::rule([
            'left_menu_name|左侧菜单名称' => 'require',
            'left_menu_desc|左侧菜单描述' => 'require',
            'left_menu_code|左侧菜单号码' => 'require',
            'staff_id|操作员id' => 'require',
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

        $params['add_time']=$params['update_time']=time();
        $result=LeftMenuModel::create($params);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    
    //后台左侧菜单编辑
    public function update()
    {
        $params = Request::param();
        
        $validate = Validate::rule([
            'left_menu_id|左侧菜单id' => 'require',
            'left_menu_name|左侧菜单名称' => 'require',
            'left_menu_desc|左侧菜单描述' => 'require',
            'left_menu_code|左侧菜单号码' => 'require',
            'staff_id|操作员id' => 'require',
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

        $leftMenuOne = LeftMenuModel::find($params['left_menu_id']);

        if ($params['left_menu_name']){
            $leftMenuOne['left_menu_name']=$params['left_menu_name'];
        }
        if ($params['left_menu_desc']){
            $leftMenuOne['left_menu_desc']=$params['left_menu_desc'];
        }
        if ($params['left_menu_code']){
            $leftMenuOne['left_menu_code']=$params['left_menu_code'];
        }
        if ($params['staff_id']){
            $leftMenuOne['staff_id']=$params['staff_id'];
        }
        
        $leftMenuOne['update_time'] = time();
        $result = $leftMenuOne->save();

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    
    //左侧菜单删除
    public function delete()
    {
        $leftMenuId = Request::param("left_menu_id");
        $validate = Validate::rule([
            'left_menu_id|左侧菜单id' => 'require|between:1,' . PHP_INT_MAX,
        ]);
        if (!$validate->check(['left_menu_id' => $leftMenuId])) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

        $result = LeftMenuModel::destroy($leftMenuId);

        $data = [
            'status' => $result ? 0 : 10001,
            'message' => $result ? '' : '修改数据库失败',
            'data' => [
                'result' =>$result ? true : false
            ]
        ];
        return json($data);
    }
    
}